package org.jederik.studip.android.view;

import org.jederik.android.provider.StudIP.Messages;
import org.jederik.android.provider.StudIP.MessagesSent;
import org.jederik.studip.android.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

/**
 * Activity zum verfassen einer Nachricht
 * @author Jann-Frederik Laß
 *
 */
public class SendMessage extends Activity {

	private static final String TAG = "SendMessage";

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        // Ladesymbol in der Titelleiste anfordern (noch nicht anzeigen):
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        
		// Layout laden:
		setContentView(R.layout.send_message);

		// Listener für den "Send"-Butoon registrieren:
		Button b = (Button) findViewById(R.id.send);
		b.setOnClickListener(
				new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						commit();
					}
				}
		);
		
		// Ggf. Eingabefelder füllen:
		Intent i = getIntent();
		if (i.hasExtra("recipient")) {
			EditText recipient = (EditText) findViewById(R.id.recipient);
			recipient.setText(i.getStringExtra("recipient"));
			EditText subject = (EditText) findViewById(R.id.subject);
			subject.requestFocus();
		}
		if (i.hasExtra("subject")) {
			EditText subject = (EditText) findViewById(R.id.subject);
			subject.setText(i.getStringExtra("subject"));
			EditText text = (EditText) findViewById(R.id.text);
			text.requestFocus();
		}
		if (i.hasExtra("text")) {
			EditText text = (EditText) findViewById(R.id.text);
			text.setText(i.getStringExtra("text"));
		}
		
	}
	
	/** 
	 * Fügt in einem seperaten Thread geschriebene Nachricht in den
	 * ContentProvider ein und schließt die Activity.
	 */
	private class Sender extends AsyncTask<ContentValues, Object, Object> {

		@Override
		protected void onPreExecute() {
			setProgressBarIndeterminateVisibility(true);
		}

		@Override
		protected Object doInBackground(ContentValues... args) {
			for (ContentValues values : args) {
				getContentResolver().insert(MessagesSent.CONTENT_URI, values);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			finish();
		}
		
	};
	
	/** Versendet die geschriebene Nachricht. */
	private void commit() {

		EditText et;
		
		// Empfänger auslesen:
		et = (EditText) findViewById(R.id.recipient);
		String recipient = et.getText().toString();
		
		// Betreff auslesen:
		et = (EditText) findViewById(R.id.subject);
		String subject = et.getText().toString();
		
		// Text der Nachricht auslesen:
		et = (EditText) findViewById(R.id.text);
		String text = et.getText().toString();
		
		// Zu Datensatz zusammenfassen:
		final ContentValues values = new ContentValues();
		values.put(Messages.RECIPIENT, recipient);
		values.put(Messages.SUBJECT, subject);
		values.put(Messages.TEXT, text);
		
		new Sender().execute(values);
		
	}
	
}
